import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
import { WishlistItem, WishlistStore } from "./type";

interface ActionsWishlistStore {
  addToWishlist: (item: WishlistItem) => void;
  removeFromWishlist: (id: number) => void;
}

const useWishlistStore = create<WishlistStore & ActionsWishlistStore>()(
  persist(
    (set) => ({
      items: [],
      addToWishlist: (item) =>
        set((state) => ({ items: [...state.items, item] })),
      removeFromWishlist: (id) =>
        set((state) => ({
          items: state.items.filter((item) => item.id !== id),
        })),
    }),
    {
      name: "wishlist-store",
      storage: createJSONStorage(() => localStorage),
    }
  )
);

export default useWishlistStore;
